package com.pingan.license.utils;


import com.pingan.license.model.LicenseCheckModel;
import com.pingan.license.model.LicenseCreateModel;
import de.schlichtherle.license.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;


import javax.security.auth.x500.X500Principal;
import java.io.File;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.prefs.Preferences;

/**
 * @AUTHOR:LIUCHAO;
 * @DATE: 2020/12/1 11:03
 */
@Component
public class LicenseMake {
    @Autowired
    private LicenseCreateModel licenseCreateModel;
    /**
     * 额外的服务器硬件校验信息
     */
    @Autowired
    private LicenseCheckModel licenseCheckModel;


    // X500Princal是一个证书文件的固有格式，详见API
    private final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal("CN=pingan, OU=pingan, O=pingan, L=chengdu, ST=chengdu, C=china");

    /**
     * 初始化证书的相关参数
     *
     * @return
     */
    @Bean
    private LicenseParam initLicenseParams() {
        Class<LicenseMake> clazz = LicenseMake.class;
        Preferences pre = Preferences.userNodeForPackage(clazz);
        // 设置对证书内容加密的对称密码
        CipherParam cipherParam = new DefaultCipherParam(licenseCreateModel.getKeyStorePwd());
        // 参数1,2从哪个Class.getResource()获得密钥库;
        //参数3密钥库的别名;
        //参数4密钥库存储密码;
        //参数5密钥库密码
        KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(clazz, licenseCreateModel.getPriPath(), licenseCreateModel.getPriAlias(), licenseCreateModel.getKeyStorePwd(), licenseCreateModel.getPrivateKeyPwd());
        // 返回生成证书时需要的参数
        LicenseParam licenseParam = new DefaultLicenseParam(licenseCreateModel.getSubject(), pre, privateStoreParam, cipherParam);
        return licenseParam;
    }

    /**
     * 通过外部配置文件构建证书的的相关信息
     *
     * @return
     * @throws ParseException
     */
    @Bean
    public LicenseContent buildLicenseContent() throws ParseException {
        LicenseContent content = new LicenseContent();
        SimpleDateFormat formate = new SimpleDateFormat("yyyy-MM-dd");

        content.setConsumerAmount(licenseCreateModel.getConsumerAmount());
        content.setConsumerType(licenseCreateModel.getConsumerType());
        content.setHolder(DEFAULTHOLDERANDISSUER);
        content.setIssuer(DEFAULTHOLDERANDISSUER);
        content.setIssued(formate.parse(licenseCreateModel.getIssued()));
        content.setNotBefore(formate.parse(licenseCreateModel.getNotBefore()));
        content.setNotAfter(formate.parse(licenseCreateModel.getNotAfter()));
        content.setInfo(licenseCreateModel.getInfo());
        //扩展校验服务器硬件信息
        content.setExtra(licenseCheckModel);
        return content;
    }


    /**
     * 生成证书，在证书发布者端执行
     *
     * @throws Exception
     */
    @Bean
    public void create() throws Exception {
        LicenseManager licenseManager = LicenseManagerHolder.getLicenseManager(initLicenseParams());
        LicenseContent content = buildLicenseContent();
        licenseManager.store(content, new File(licenseCreateModel.getLicPath()));
        System.out.println("证书发布成功");
    }
}
